<template>
  <div class="SysDiscuss">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>评论管理</span>
      </div>
      <div class="SysDiscussForm">
        <el-table :data="commentData" style="width: 100%" :default-sort = "{prop: 'create_time', order: 'descending'}">
          <el-table-column label="日期" sortable width="220" prop="create_time"></el-table-column>
          <el-table-column label="ID" width="220" prop="id"></el-table-column>
          <el-table-column label="文章ID" width="220" prop="blog_id"></el-table-column>
          <el-table-column label="内容" width="220">
            <template slot-scope="scope">
              <div>{{ scope.row.content }}</div>
            </template>
          </el-table-column>
          <el-table-column label="作者" width="220">
            <template slot-scope="scope">
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.nickname }}</el-tag>
                </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage1"
        :page-sizes="pageSizeArr"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
import { getCommentsApi,delCommentApi } from '@/api/comment';
export default {
  data() {
    return {
      currentPage1: 1,
      commentData: [
      ],
      pageSizeArr:[5,10,20,40,80],
      total:0,
      pageSize:'',
    };
  },
  methods: {
    async getComments(data){
        if (data) {
        await getCommentsApi(data).then(res=>{
            this.commentData = res.data.comments
            this.total = res.data.total
        })
      } else {
        const data = { pageSize: "", pageNum: "" };
        await getCommentsApi(data).then(res=>{
            this.commentData = res.data.comments
            this.total = res.data.total
        })
      }
        
    },
    handleEdit(index, row) {
        this.$alert(row.content, '标题名称', {
          confirmButtonText: '确定',
          callback: action => {
            this.$message({
              type: 'info',
              message: `action: ${ action }`
            });
          }
        });
    },
    async handleDelete(index, row) {
        this.$confirm('此操作将永久删除该文件--row.content, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
            const data = {comment_id:row.id,isdel:0}
            await delCommentApi(data).then(async res=>{
            if(res.data.code==200){
                const data = { pageSize: this.pageSize, pageNum: this.currentPage1 };
            await getCommentsApi(data).then(res=>{
                this.commentData = res.data.comments
                this.total = res.data.total
            })
        }
       })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    async handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val
        const data = { pageSize: this.pageSize, pageNum: this.currentPage1 };
        await getCommentsApi(data).then(res=>{
            this.commentData = res.data.comments
            this.total = res.data.total
        })
      },
      async handleCurrentChange(val) {
        this.currentPage1 = val
        const data = { pageSize: this.pageSize, pageNum: this.currentPage1 };
        await getCommentsApi(data).then(res=>{
            this.commentData = res.data.comments
            this.total = res.data.total
        })
      }
  },
  mounted(){
    this.getComments()
  }
};
</script>
<style lang="less" scoped>
.SysDiscuss {
  height: 80vh;
}
.SysDiscussForm {
  height: 70vh;
  overflow: auto;
}
.commentFace {
  width: 40px;
  height: 40px;
}
</style>